package com.yaxi.multipkid.filter;

import android.text.Spanned;

/**
 * 最大小数位长度过滤器
 *
 * @author yaxi
 * @date 2019/1/10
 */
public class DecimalLengthFilter extends BaseFilter {


    private int maxDecimalLength = 0;
    private boolean replaceMultiple = true;

    public DecimalLengthFilter(int maxDecimalLength) {
        if (maxDecimalLength < 0) {
            maxDecimalLength = 0;
        }
        this.maxDecimalLength = maxDecimalLength;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        if (maxDecimalLength == 0 && contains(source, DIGITS_POINT)) {
            return "";
        }
        //禁止粘贴多个数字  下面代码有问题，请注意，如果是直接给view赋值。如settext（String）也会被过滤掉  有时间测试一下
//        if (!replaceMultiple && source.length() > 1) {
//            return "";
//        }
        //目标值没有小数点
        if (!contains(dest, DIGITS_POINT)) {
            //如果输入的包含小数点
            if (contains(source, DIGITS_POINT)) {

                //输入源的小数点位数
                int sourceDecimalLength = source.length() - indexOf(source, DIGITS_POINT) - 1;

                int beforeInsertDestDecimalLength = dest.length() - dend - 1;
                if (beforeInsertDestDecimalLength > maxDecimalLength) {
                    return "";
                }
                int afterInsertDestDecimalLength = beforeInsertDestDecimalLength + sourceDecimalLength;
                if (afterInsertDestDecimalLength > maxDecimalLength) {
                    int ignoreTail = afterInsertDestDecimalLength - maxDecimalLength;
                    return source.subSequence(0, source.length() - ignoreTail);
                }
                return source;//return null ?
            } else {
                return source;
            }
        } else {//小数点在输入框的数字中 || destValue.indexOf(DIGITS_POINT) == destValue.length() -1
            if (indexOf(dest, DIGITS_POINT) < dstart) {
                if (contains(source, DIGITS_POINT)) {
                    return "";
                } else {
                    int destDecimals = dest.length() - indexOf(dest, DIGITS_POINT) - 1;
                    if (destDecimals + end - start > maxDecimalLength) {
                        int ignoreTail = destDecimals + end - start - maxDecimalLength;
                        return source.subSequence(0, source.length() - ignoreTail);
                    }
                }
            } else if (indexOf(dest, DIGITS_POINT) > dend) {
                if (contains(source, DIGITS_POINT)) {
                    return "";
                }
            } else {
                if (contains(source, DIGITS_POINT)) {
                    if (source.length() - 1 - indexOf(source, DIGITS_POINT) + dest.length() - 1 - dend > maxDecimalLength) {
                        int ignoreTail = source.length() - 1 - indexOf(source, DIGITS_POINT) + dest.length() - 1 - dend - maxDecimalLength;
                        return source.subSequence(0, source.length() - ignoreTail);
                    }
                }
            }
        }
        //表示不过滤
        return null;
    }


}
